//
//  personIndexView.m
//  Estate
//
//  Created by heyong on 16/5/27.
//  Copyright © 2016年 ttsx. All rights reserved.
//

#import "personIndexView.h"

@implementation personIndexView
- (UITableView *)tableView{
    if(_tableView==nil){
        _tableView=[[UITableView alloc]init];
    }
    return _tableView;
}
- (UIButton *)exitBtn{
    if(_exitBtn==nil){
        _exitBtn=[[UIButton alloc]init];
    }
    return _exitBtn;
}
-(UILabel *)lab_1{
    if(_lab_1==nil){
        _lab_1=[[UILabel alloc]init];
    }
    return _lab_1;
}

-(UILabel *)lab_2{
    if(_lab_2==nil){
        _lab_2=[[UILabel alloc]init];
    }
    return _lab_2;
}

-(void)setModel:(personModel *)model{
    _model=model;
    /*
     *  甲方  巡检员  巡检组长 显示智慧监理
     */
    NSString *role=DEF_PERSISTENT_GET_OBJECT(@"role");
    if([USERTYPE.typeArray containsObject:role]||[USERTYPE.typeArrayNoWeather containsObject:role]){
        if([role isEqualToString:@"BUILD_01"]||[role isEqualToString:@"BUILD_02"]){
            self.lab_1.text=_model.projectName;
        }else{
            self.lab_1.text=@"智慧监理";
        }
    }else{
        if(!_model.projectName){
            self.lab_1.text=@"智慧监理";
        }else{
            self.lab_1.text=_model.projectName;
            
        }
    }
    self.lab_2.text=[NSString stringWithFormat:@"%@|%@",_model.position,_model.name];
    
//    NSString *urlStr=@"";
//    //判断头像是否为空
//    if([_model.headImagePath isEqualToString:@""]||!_model.headImagePath){
//        NSString *header=DEF_PERSISTENT_GET_OBJECT(@"headImage");
//        
//        _model.headImagePath=header;
//        urlStr=[NSString stringWithFormat:@"%@/%@",API_HOST,_model.headImagePath];
//        
//        [self.txImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"touxiang_de"] ];
//        
//    }else{
//        urlStr=[NSString stringWithFormat:@"%@/%@",API_HOST,_model.headImagePath];
//        [self.txImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"touxiang_de"]];
//    }
    
    [LSHttpTool checkNetWork:^(NSString *status) {
        if([status isEqualToString:@"0"]){
            NSString *header=DEF_PERSISTENT_GET_OBJECT(@"headImage");
            
            _model.headImagePath=header;
            
            NSString *urlStr=[NSString stringWithFormat:@"%@/%@",API_HOST,_model.headImagePath];
            [self.txImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"touxiang_de"] options:SDWebImageRefreshCached];
            
        }else{
            NSString* str=[_model.headImagePath stringByAppendingString:[NSString stringWithFormat:@"&r=%@",[LSUtility getCurrentDateString]]];
            
            NSString *urlStr=[NSString stringWithFormat:@"%@/%@",API_HOST,str];
            DEF_PERSISTENT_SET_OBJECT( str, @"headImage");
            [self.txImageView sd_setImageWithURL:[NSURL URLWithString:urlStr] placeholderImage:[UIImage imageNamed:@"touxiang_de"] options:SDWebImageRefreshCached];
        }
    }];

    
}

- (UIImageView *)txImageView{
    if(!_txImageView){
        _txImageView=[[UIImageView alloc]init];
    }
    return _txImageView;
}

- (instancetype)initWithFrame:(CGRect)frame{
    if(self=[super initWithFrame:frame]){
        [self createView];
    }
    return self;
}


- (void)createView{
    WEAKSELF
    headImage=[[UIImageView alloc]initWithFrame:CGRECTS(0, 0, 375, 150)];
    //    headImage.image=[UIImage imageNamed:@""];
    headImage.image=[UIImage imageNamed:@"beijing_1"];
    [self addSubview:headImage];
    
    //添加头像视图
    self.txImageView .backgroundColor=[UIColor whiteColor];
    [headImage addSubview:self.txImageView ];
    [self.txImageView  mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(headImage.mas_centerY).offset(10*BIZ);
        make.left.offset(20*BIZ);
        make.width.offset(70*BIZ);
        make.height.offset(70*BIZ);
    }];
    self.txImageView .layer.cornerRadius=35.*BIZ;
    self.txImageView .layer.masksToBounds=YES;
    self.txImageView .image=[UIImage imageNamed:@"touxiang_de"];
    
    headImage.userInteractionEnabled=YES;
    self.txImageView.userInteractionEnabled=YES;
    //添加点击事件
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickImage:)];
    [self.txImageView addGestureRecognizer: tap];
    
    
    //添加项目名
    self.lab_1.textColor=[UIColor whiteColor];
    self.lab_1.font=[UIFont systemFontOfSize:16.*BIZ];
    [headImage addSubview:self.lab_1];
    [self.lab_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.txImageView.mas_right).offset(10*BIZ);
        make.top.equalTo(weakSelf.txImageView.mas_top).offset(18*BIZ);
        make.right.offset(-20*BIZ);
        make.height.offset(10*BIZ);
    }];
    //添加名称
    self.lab_2.textColor=[UIColor whiteColor];
    self.lab_2.font=[UIFont systemFontOfSize:16.*BIZ];
    [headImage addSubview:self.lab_2];
    [self.lab_2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.txImageView.mas_right).offset(30*BIZ);
        make.bottom.equalTo(weakSelf.txImageView.mas_bottom).offset(-18*BIZ);
        make.right.mas_offset(-10);
        make.height.offset(15*BIZ);
    }];
    
    UIImageView *img_1=[[UIImageView alloc]init];
    img_1.image=[UIImage imageNamed:@"renwu"];
    [headImage addSubview:img_1];
    [img_1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.txImageView.mas_right).offset(10*BIZ);
        make.centerY.mas_equalTo(weakSelf.lab_2.mas_centerY);
        make.width.offset(15*BIZ);
        make.height.offset(15*BIZ);
    }];
    
    //添加右上方图标
    //    UIImageView *img_2=[[UIImageView alloc]init];
    //    img_2.image=[UIImage imageNamed:@"xunfangjilu"];
    //    [headImage addSubview:img_2];
    //    [img_2 mas_makeConstraints:^(MASConstraintMaker *make) {
    //        make.right.mas_equalTo(-20*BIZ);
    //        make.bottom.mas_equalTo(txImg.mas_top).offset(10*BIZ);
    //        make.width.offset(20*BIZ);
    //        make.height.offset(20*BIZ);
    //    }];
    //
    //添加个人信息表格
    [self addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(headImage.mas_bottom);
        make.left.offset(0);
        make.right.offset(0);
        make.height.mas_equalTo(self.mas_height).multipliedBy(0.6);
    }];
    self.tableView.backgroundColor=[UIColor colorWithRed:0.95 green:0.96 blue:0.96 alpha:1.00];
    self.tableView.bounces=NO;
    [self.tableView registerNib:[UINib nibWithNibName:@"PersonIndexCell" bundle:nil] forCellReuseIdentifier:@"perCell"];
    [LSUtility setExtraCellLineHidde:self.tableView tabHeader:NO tabFooter:NO];
    
    // 退出当前账号
    self.exitBtn.backgroundColor=[UIColor colorWithRed:0.92 green:0.29 blue:0.21 alpha:1.00];
    self.exitBtn.layer.cornerRadius = 4.;
    [self.exitBtn setTitle:@"退出当前账号" forState:UIControlStateNormal];
    [self addSubview:self.exitBtn];
    [self.exitBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.tableView.mas_bottom).offset(20);
        make.centerX.mas_equalTo(self.mas_centerX);
        make.width.mas_equalTo(self.mas_width).multipliedBy(0.8);
        make.height.offset(44*BIZ);
    }];
}

-(void)clickImage:(UIGestureRecognizer *)ges{
    UIImageView *imgv=(UIImageView *)ges.view;
    [LSTarBrowser showImage:imgv];
}

@end
